package com.azarias.test;

import com.azarias.controller.RegisterServerController;
import com.azarias.model.vo.HeartbeatRequest;
import com.azarias.model.vo.RegisterRequest;
import com.azarias.model.vo.RegisterResponse;
import com.azarias.monitor.ServiceAliveMonitor;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

/**
 * @description: some desc
 * @author: Azarias_Jx
 * @date: 2022/1/2 15:58
 */
@Slf4j
public class RegisterServer {
    public static void main(String[] args) throws InterruptedException {
        RegisterServerController controller = new RegisterServerController();


        // 模拟发起一个服务注册的请求
        RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.setHostname("inventory-service-01");
        registerRequest.setIp("192.168.31.208");
        registerRequest.setPort(9000);
        registerRequest.setServiceName("inventory-service");

         RegisterResponse registerResponse= controller.register(registerRequest);
         String serviceInstanceId = registerResponse.getServiceInstanceId();

        // 模拟进行一次心跳，完成续约
        HeartbeatRequest heartbeatRequest = new HeartbeatRequest();
        heartbeatRequest.setServiceName("inventory-service");
        heartbeatRequest.setServiceInstanceId(serviceInstanceId);

        controller.heartbeat(heartbeatRequest);

        // 开启一个后台线程，检测微服务的存活状态
        ServiceAliveMonitor serviceAliveMonitor = new ServiceAliveMonitor();
        serviceAliveMonitor.start();

        while (true) {
            Thread.sleep(30 * 1000);
        }
    }
}
